package cn.ddiancan.xddcloud.psc.flow.engine.service.impl;

import java.util.List;
import java.util.Optional;

import cn.ddiancan.xddcloud.psc.flow.engine.service.QueryService;
import cn.ddiancan.xddcloud.psc.flow.entity.XddExtInstance;
import cn.ddiancan.xddcloud.psc.flow.entity.XddHisInstance;
import cn.ddiancan.xddcloud.psc.flow.entity.XddHisTask;
import cn.ddiancan.xddcloud.psc.flow.entity.XddHisTaskActor;
import cn.ddiancan.xddcloud.psc.flow.entity.XddInstance;
import cn.ddiancan.xddcloud.psc.flow.entity.XddTask;
import cn.ddiancan.xddcloud.psc.flow.entity.XddTaskActor;
import cn.ddiancan.xddcloud.psc.flow.feign.*;
import cn.ddiancan.xddcloud.psc.flow.feign.XddExtInstanceRemoteService;
import cn.ddiancan.xddcloud.psc.flow.feign.XddHisInstanceRemoteService;
import cn.ddiancan.xddcloud.psc.flow.feign.XddHisTaskActorRemoteService;
import cn.ddiancan.xddcloud.psc.flow.feign.XddHisTaskRemoteService;
import cn.ddiancan.xddcloud.psc.flow.feign.XddInstanceRemoteService;
import cn.ddiancan.xddcloud.psc.flow.feign.XddTaskActorRemoteService;
import cn.ddiancan.xddcloud.psc.flow.feign.XddTaskRemoteService;

/**
 * 查询服务实现类
 *
 * ，不允许非法使用，后果自负
 *
 * @since 1.0
 */
public class QueryServiceImpl implements QueryService {

    private final XddInstanceRemoteService instanceService;

    private final XddHisInstanceRemoteService hisInstanceService;

    private final XddExtInstanceRemoteService extInstanceService;

    private final XddTaskRemoteService taskService;

    private final XddTaskActorRemoteService taskActorService;

    private final XddHisTaskRemoteService hisTaskService;

    private final XddHisTaskActorRemoteService hisTaskActorService;

    public QueryServiceImpl(XddInstanceRemoteService instanceService, XddHisInstanceRemoteService hisInstanceService,
        XddExtInstanceRemoteService extInstanceService, XddTaskRemoteService taskService, XddTaskActorRemoteService taskActorService, XddHisTaskRemoteService hisTaskService,
        XddHisTaskActorRemoteService hisTaskActorService) {
        this.instanceService = instanceService;
        this.hisInstanceService = hisInstanceService;
        this.extInstanceService = extInstanceService;
        this.taskService = taskService;
        this.taskActorService = taskActorService;
        this.hisTaskService = hisTaskService;
        this.hisTaskActorService = hisTaskActorService;
    }

    @Override
    public XddInstance getInstance(Long instanceId) {
        return instanceService.selectById(instanceId);
    }

    @Override
    public XddTask getTask(Long taskId) {
        return taskService.selectById(taskId);
    }

    @Override
    public XddHisInstance getHistInstance(Long instanceId) {
        return hisInstanceService.selectById(instanceId);
    }

    @Override
    public XddExtInstance getExtInstance(Long instanceId) {
        return extInstanceService.selectById(instanceId);
    }

    @Override
    public boolean existActiveSubProcess(Long instanceId) {
        return instanceService.selectCountByParentInstanceId(instanceId) > 0;
    }

    @Override
    public XddHisTask getHistTask(Long taskId) {
        return hisTaskService.selectById(taskId);
    }

    @Override
    public Optional<List<XddHisTask>> getHisTasksByName(Long instanceId, String taskName) {
        return Optional.ofNullable(hisTaskService.selectListByInstanceIdAndTaskName(instanceId, taskName));
    }

    @Override
    public List<XddTask> getTasksByInstanceId(Long instanceId) {
        return taskService.selectListByInstanceId(instanceId);
    }

    @Override
    public List<XddTask> getTasksByInstanceIdAndTaskName(Long instanceId, String taskName) {
        return taskService.selectListByInstanceIdAndTaskName(instanceId, taskName);
    }

    @Override
    public List<XddTask> getTasksByInstanceIdAndTaskKey(Long instanceId, String taskKey) {
        return taskService.selectListByInstanceIdAndTaskKey(instanceId, taskKey);
    }

    @Override
    public Optional<List<XddTaskActor>> getActiveTaskActorsByInstanceId(Long instanceId) {
        return Optional.ofNullable(taskActorService.selectListByInstanceId(instanceId));
    }

    @Override
    public List<XddTaskActor> getTaskActorsByTaskId(Long taskId) {
        return taskActorService.selectListByTaskId(taskId);
    }

    @Override
    public List<XddTaskActor> getTaskActorsByTaskIdAndActorId(Long taskId, Long actorId) {
        return taskActorService.selectListByTaskIdAndActorId(taskId, actorId);
    }

    @Override
    public List<XddHisTaskActor> getHisTaskActorsByTaskId(Long taskId) {
        return hisTaskActorService.selectListByTaskId(taskId);
    }

    @Override
    public List<XddHisTaskActor> getHisTaskActorsByTaskIdAndActorId(Long taskId, Long actorId) {
        return hisTaskActorService.selectListByTaskIdAndActorId(taskId, actorId);
    }

    @Override
    public List<XddTask> getActiveTasks(Long instanceId, List<String> taskNames) {
        return taskService.selectListByInstanceIdAndTaskNames(instanceId, taskNames);
    }

    @Override
    public Optional<List<XddHisTask>> getHisTasksByInstanceId(Long instanceId) {
        return hisTaskService.selectListByInstanceId(instanceId);
    }

    @Override
    public Optional<List<XddInstance>> getSubProcessByInstanceId(Long instanceId) {
        return instanceService.selectListByParentInstanceId(instanceId);
    }

}
